﻿using mealplanner.database;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mealplanner.control
{
    public class menu
    {
        private string menu_name;
        public void setname(string name)
        {
            menu_name = name;
        }
        public string getname()
        {
            return menu_name;
        }

        public int checkMenuName(string menuName)
        {
            ArrayList menu = new ArrayList();
            int flage = 0;
            menu = MenuDB.getAllMenus();
            foreach (var x in menu)
            {
                if (menuName == x.ToString())
                {
                    flage = 1;
                    break;
                }
            }
            if (flage == 0)
            {
                return 0;
            }
            return 1;
        }

        public ArrayList conqure_String(string com_string)
        {
            ArrayList structure = new ArrayList();
            string temp = "", pattern;
            pattern = @"^Week 1$|^Week 2$|^Week 3$|^Week 4$|^Day 1$|^Day 2$|^Day 3$|^Day 4$|^Day 5$|^Day 6$|^Day 7$";
            string check;
            check = "^[0-9]*?$";
            for (int i = 0; i < com_string.Length; i++)
            {
                temp = temp + com_string[i];
                if (Regex.IsMatch(temp, pattern))
                {
                    structure.Add(temp);
                    temp = "";
                }
                else if (Regex.IsMatch(temp, check))
                {
                    structure.Add(temp);
                    temp = "";
                }
            }
            
            return structure;
        }

        void addMenuMeals(string menuName, ArrayList Meals)
        {
            if (checkMenuName(menuName) == 1)
            {
                ArrayList Ids = new ArrayList();
                ArrayList temp = new ArrayList();
                int menuId;
                for (int i = 0; i < Meals.Count; i++)
                {
                    Ids.Add(MealDB.getMealIdByMealName(Meals[i].ToString())[0]);
                }
                temp.Add(MenuDB.getMenuIdByMenuName(menuName)[0]);
                menuId = int.Parse(temp[0].ToString());
                MenuDB.addMenuMeals(menuId, Ids);
            }
            else {
                throw new ArgumentException("Menu Name Not Valid");
            }
        }

        public void add_menu(ArrayList menu)
        {


            string pattern, structure = "";
            int count = 0;
            ArrayList meal = new ArrayList();
            pattern = @"^Week 1$|^Week 2$|^Week 3$|^Week 4$|^Day 1$|^Day 2$|^Day 3$|^Day 4$|^Day 5$|^Day 6$|^Day 7$";
            for (int i = 0; i < menu.Count; i++)
            {
                try
                {
                    if (Regex.IsMatch(menu[i].ToString(), pattern))
                    {
                        if (count != 0)
                        {
                            structure = structure + count.ToString();
                        }
                        structure = structure + menu[i].ToString();
                        count = 0;
                    }

                    else
                    {
                        count++;
                        meal.Add(menu[i]);
                    }
                }
                catch {
                    if (menu[i] == null) {
                        throw new ArgumentException("There was an unhandled null");
                    }
                }
            }
            if (count != 0)
            {
                structure = structure + count.ToString();
            }

            //foreach (var x in meal)
            //{
            //    MessageBox.Show(x.ToString());
            //}
            //MessageBox.Show(structure);
            MenuDB.addMenu(this.menu_name, DateTime.Now.ToString("yyyy-MM-dd"), structure);
            addMenuMeals(this.menu_name, meal);


        }//end function

        public ArrayList retriveMenu(string menuName)
        {
            ArrayList menu = new ArrayList();
            try
            {
                int menuId, counter = 0;
                string structure;
                ArrayList temp = new ArrayList();
                ArrayList meals = new ArrayList();
                string check/*,pattern*/;
                //pattern = @"^Week 1$|^Week 2$|^Week 3$|^Week 4$|^Day 1$|^Day 2$|^Day 3$|^Day 4$|^Day 5$|^Day 6$|^Day 7$";
                check = "^[0-9]*?$";
                menuId = int.Parse(MenuDB.getMenuIdByMenuName(menuName)[0].ToString());
                structure = MenuDB.getMenuStructureByMenuName(menuName)[0].ToString();
                temp = conqure_String(structure);
                meals = MenuDB.getMenuMeals(menuId);
                foreach (var x in temp)
                {
                    if (Regex.IsMatch(x.ToString(), check))
                    {
                        for (int i = 0; i < int.Parse(x.ToString()); i++)
                        {
                            menu.Add(meals[counter]);
                            counter++;
                        }
                    }
                    else
                    {
                        menu.Add(x.ToString());
                    }
                }
               
            }
            catch {
                if (menuName == null) {
                    throw new ArgumentException("There was an unhandled null");
                }
                else if (menuName == "")
                {
                    throw new ArgumentException("There was an empty string");
                }
                else {
                    throw new ArgumentException("Invalid menu name");
                }
            }
            return menu;
        }

        public ArrayList retriveAllMenu()
        {
            return MenuDB.getAllMenus();
        }

        public int checkStructure(ArrayList menuSoFare)
        {
            //foreach (string meal in menuSoFare)
            //{
            //    MessageBox.Show(meal);
            //}

            int flage = 0;
            string weekPattern, DayPattern;
            weekPattern = @"^Week 1$|^Week 2$|^Week 3$|^Week 4$";
            DayPattern = @"^Day 1$|^Day 2$|^Day 3$|^Day 4$|^Day 5$|^Day 6$|^Day 7$";
            for (int i = 0; i < menuSoFare.Count; i++)
            {
                //if (Regex.IsMatch(menuSoFare[i].ToString(), DayPattern) && menuSoFare.Count > 1)
                //{
                //    if ((i + 1) < menuSoFare.Count && Regex.IsMatch(menuSoFare[i + 1].ToString(), weekPattern))
                //    {
                //        //MessageBox.Show("week after day not avalible");
                //        return -1;
                //    }
                //}
                if (Regex.IsMatch(menuSoFare[i].ToString(), weekPattern))
                {
                    for (int j = i + 1; j < menuSoFare.Count; j++)
                    {
                        if (menuSoFare[j].ToString() == menuSoFare[i].ToString())
                        {
                            //MessageBox.Show("tow selection of the same not avalible");
                            return 0;
                        }
                    }
                    flage = 1;
                }
                if (flage == 0) {
                    return -1;
                }
                if (Regex.IsMatch(menuSoFare[i].ToString(), DayPattern)) {
                    for (int j = i + 1; j < menuSoFare.Count; j++)
                    {
                        if (menuSoFare[j].ToString() == menuSoFare[i].ToString())
                        {
                            //MessageBox.Show("tow selection of the same not avalible");
                            return 0;
                        }
                        else if (Regex.IsMatch(menuSoFare[j].ToString(), weekPattern)) {
                            break;
                        }
                    }
                }
            }

            //MessageBox.Show("right choice");
            return 1;
        }

        public void deleteMenu(string menuName) {
            if (checkMenuName(menuName) == 1)
            {
                MenuDB.deleteMenuByName(menuName);
            }
            else {
                throw new ArgumentException("Name not valid");
            }
        }

        public void deleteSpecificDetailsFromMenu(string menuName,ArrayList deletedDetails) {
            if (checkMenuName(menuName) == 1)
            {
                ArrayList menuExist = retriveMenu(menuName);
                deleteMenu(menuName);
                int counter = 0;
                for (int i = 0; i < menuExist.Count; i++)
                {
                    try
                    {
                        if (counter < deletedDetails.Count && menuExist[i].ToString() == deletedDetails[counter].ToString())
                        {
                            menuExist.RemoveAt(i);
                            i--;
                            counter++;
                        }
                    }
                    catch {
                        if (deletedDetails[i] == "")
                        {
                            throw new ArgumentException("There Was Empty String");
                        }
                        else if (deletedDetails[i] == null)
                        {
                            throw new ArgumentException("There was an unhandled null");
                        }
                        else
                        {
                            throw new ArgumentException("Invalid Insert Name");
                        }
                    }
                }
                this.setname(menuName);
                add_menu(menuExist);
            }
        }

        public void updateMenu(string menuName,ArrayList changes) {
            if (checkMenuName(menuName) == 1)
            {
                string currentStructure = MenuDB.getMenuStructureByMenuName(menuName)[0].ToString();
                string pattern, structure = "";
                int count = 0;
                ArrayList meal = new ArrayList();
                pattern = @"^Week 1$|^Week 2$|^Week 3$|^Week 4$|^Day 1$|^Day 2$|^Day 3$|^Day 4$|^Day 5$|^Day 6$|^Day 7$";
                for (int i = 0; i < changes.Count; i++)
                {
                    
                        if (Regex.IsMatch(changes[i].ToString(), pattern))
                        {
                            if (count != 0)
                            {
                                structure = structure + count.ToString();
                            }
                            structure = structure + changes[i].ToString();
                            count = 0;
                        }
                        else
                        {
                            count++;
                            meal.Add(changes[i]);
                        }
                    
                    
                }//end of for loop
                if (count != 0)
                {
                    structure = structure + count.ToString();
                }

                structure = currentStructure + structure;
                //foreach (var x in meal)
                //{
                //    MessageBox.Show(x.ToString());
                //}
                //MessageBox.Show(structure);
                int menuId = int.Parse(MenuDB.getMenuIdByMenuName(menuName)[0].ToString());
                //MessageBox.Show(menuId.ToString());
                MenuDB.updateMenuStructure(menuId, structure);
                addMenuMeals(menuName, meal);
            }
        }
    }
}
